package com.android.plc.ui.control;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.plc.CONST;
import com.android.plc.CONST.COMMAND;
import com.android.plc.R;

public class AirControl extends NormalControl implements OnClickListener
{
	private static final int DAKIN = 0x02;
	private Button mPushBtn;
	private Button mReduceBtn;
	private TextView mIndoorTv;
	private TextView mSetTv;
	private TextView mStatusTv;
	private TextView mHotTv;
	private TextView mLowTv;
	private Button mBtnLowWind;
	private Button mBtnStongWind;
	private Button mBtnAir;
	private Button mBtnHeating;
	private Button mBtnRefrig;
	private Button mBtnDehum;

	private int mSetTemp = 26;
	private int mLastCommand = -1;

	public AirControl(Context context, ViewGroup view, int res_id)
	{
		super(context, view, res_id);
	}

	public AirControl(Context context, ViewGroup view)
	{
		super(context, view, R.layout.control_air);
	}

	@Override
	public void initView(ViewGroup view)
	{
		super.initView(view);

		mPushBtn = (Button) view.findViewById(R.id.btn_push);
		mReduceBtn = (Button) view.findViewById(R.id.btn_reduce);
		mIndoorTv = (TextView) view.findViewById(R.id.indoor_temperature);
		mSetTv = (TextView) view.findViewById(R.id.set_temperature);
		mStatusTv = (TextView) view.findViewById(R.id.status);
		mHotTv = (TextView) view.findViewById(R.id.hot);
		mLowTv = (TextView) view.findViewById(R.id.low);

		mBtnLowWind = (Button) view.findViewById(R.id.btn_low_wind);
		mBtnStongWind = (Button) view.findViewById(R.id.btn_strong_wind);
		mBtnAir = (Button) view.findViewById(R.id.btn_air);
		mBtnHeating = (Button) view.findViewById(R.id.btn_heating);
		mBtnRefrig = (Button) view.findViewById(R.id.btn_refrigeration);
		mBtnDehum = (Button) view.findViewById(R.id.btn_dehumidification);

		mPushBtn.setOnClickListener(this);
		mReduceBtn.setOnClickListener(this);

		mBtnLowWind.setOnClickListener(this);
		mBtnStongWind.setOnClickListener(this);

		mBtnAir.setOnClickListener(this);
		mBtnHeating.setOnClickListener(this);

		mBtnRefrig.setOnClickListener(this);
		mBtnDehum.setOnClickListener(this);

		mLastCommand = CONST.AIRCommand.Query_All;
		sendCommand(COMMAND.EXTEND, DAKIN, mLastCommand);
	}

	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_off:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Off);
			break;
		case R.id.btn_on:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_On);
			break;
		case R.id.btn_air:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Mode0);
			break;
		case R.id.btn_heating:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Mode1);
			break;
		case R.id.btn_refrigeration:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Mode2);
			break;
		case R.id.btn_dehumidification:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Mode4);
			break;
		case R.id.btn_low_wind:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Speed_L);
			break;
		case R.id.btn_strong_wind:
			sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Speed_H);
			break;
		case R.id.btn_push:
			if (mSetTemp < 32)
			{
				mSetTemp++;
				sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Tempratue);
				sendCommandDelay(COMMAND.EXTENDDATA, 0, mSetTemp);
				mSetTv.setText(String.valueOf(mSetTemp));
			}
			break;
		case R.id.btn_reduce:
			if (mSetTemp > 16)
			{
				mSetTemp--;
				sendCommand(COMMAND.EXTEND, DAKIN, CONST.AIRCommand.Set_Tempratue);
				sendCommandDelay(COMMAND.EXTENDDATA, 0, mSetTemp);
				mSetTv.setText(String.valueOf(mSetTemp));
			}
			break;
		default:
			break;
		}
	}

	private int getOnRes(int ison)
	{
		int status_id;
		if (ison == CONST.AIRCommand.Set_On)
		{
			status_id = R.string.satus_on;
		} else
		{
			status_id = R.string.satus_off;
		}
		return status_id;
	}

	private int getWindRes(int low)
	{
		int low_id;
		switch (low)
		{
		case CONST.AIRCommand.Set_Speed_L:
			low_id = R.string.low_wind;
			break;
		default:
			low_id = R.string.strong_wind;
			break;
		}
		return low_id;
	}

	private int getModeRes(int mode)
	{
		int hot_id;
		switch (mode)
		{
		case CONST.AIRCommand.Set_Mode0:
			hot_id = R.string.air;
			break;
		case CONST.AIRCommand.Set_Mode1:
			hot_id = R.string.heating;
			break;
		case CONST.AIRCommand.Set_Mode2:
			hot_id = R.string.refrigeration;
			break;
		case CONST.AIRCommand.Set_Mode3:
			hot_id = R.string.auto;
			break;
		case CONST.AIRCommand.Set_Mode4:
			hot_id = R.string.dehumidification;
			break;
		default:
			hot_id = R.string.auto;
			break;
		}
		return hot_id;
	}

	@Override
	public void onReceive(int command, int data1, int data2)
	{
		if (command == COMMAND.EXTENDDATAFEEDBACK)
		{
			int status = (data1 << 8) + data2;
			Log.w(TAG, "rev socket COMMAND.EXTENDDATAFEEDBACK " + Integer.toHexString(status));
			int ison = status & 0x01;
			Log.w(TAG, "rev socket Air On " + ison);
			status = status >> 1;
			int hot = status & 0x07;
			Log.w(TAG, "rev socket Air hot " + hot);
			status = status >> 3;
			int low = status & 0x03;
			Log.w(TAG, "rev socket Air low " + low);
			status = status >> 2;
			int set = (status & 0x0F) + 16;
			Log.w(TAG, "rev socket Air set " + set);
			status = status >> 4;
			int in = (status & 0x2F) - 16;
			Log.w(TAG, "rev socket Air in " + in);
			Log.e(TAG, "rev socket Tempratue/Set =" + set + " Indoor" + in);
			mSetTemp = set;
			mSetTv.setText(String.valueOf(set));
			mIndoorTv.setText(String.valueOf(in));

			super.onReceive(ison == 0 ? COMMAND.OFF : COMMAND.ON, 0, 0);
			mStatusTv.setText(getOnRes(ison + CONST.AIRCommand.Set_Off));
			mHotTv.setText(getModeRes(hot + CONST.AIRCommand.Set_Mode0));

			mLowTv.setText(getWindRes(low + CONST.AIRCommand.Set_Speed_L));
		} else if (command == COMMAND.EXTEND)
		{
			if (data2 >= CONST.AIRCommand.Set_Mode0 && data2 <= CONST.AIRCommand.Set_Mode4)
			{
				mHotTv.setText(getModeRes(data2));
			} else if (data2 >= CONST.AIRCommand.Set_Speed_L && data2 <= CONST.AIRCommand.Set_Speed_H)
			{
				mLowTv.setText(getWindRes(data2));
			} else if (data2 >= CONST.AIRCommand.Set_Off && data2 <= CONST.AIRCommand.Set_On)
			{
				super.onReceive(data2 == CONST.AIRCommand.Set_On ? COMMAND.ON : COMMAND.OFF, 0, 0);
				mStatusTv.setText(getOnRes(data2));
			}
		} else if (command == COMMAND.EXTENDDATA)
		{
			mSetTemp = data2;
			mSetTv.setText(String.valueOf(data2));
		}

	}
}
